<template>
	<layout :loading="loading" pageKey="article" title="文章管理">
		
		<block slot="titleLeft">
			<conditions :showCount="1" :conditions="conditions" :list="conditonList" @confirm="submitSearch"></conditions>
		</block>
		
		<block slot="titleRight">
			<auth-nav href="/pages/article/edit" url="admin/article/info" class="btn w80 flex">
				<text class="bIcon-addCircle mr5"></text>
				添加
			</auth-nav>
		</block>
		<tables :list="page.list">
			<block slot="thead">
				<th>文章标题</th>
				<th>文章分类</th>
				<th>时间</th>
				<th>是否置顶</th>
				<th>阅读加精</th>
				<th>阅读量</th>
				<th>点赞数</th>
				<th>状态</th>
				<th>操作</th>
			</block>
			<block slot="tbody">
				<tr v-for="( x , index) in page.list" :key="index">
					<td>
						{{x.title}}
					</td>
					<td>
						{{x.category_id}}
					</td>
					<td>
						{{x.createTimeStr}}
					</td>
					<td>
						{{x.is_sticky}}
					</td>
					<td>
						{{x.is_essence}}
					</td>
					<td>
						{{x.view_count}}
					</td>
					<td>
						{{x.like_count}}
					</td>
					<td>
						{{x.article_status}}
					</td>
					<td>
						<auth-nav :href="`/pages/article/edit?id=${x._id}`" url="admin/article/info" class="main bold plr5">
							编辑
						</auth-nav>
						<auth-btn :url="`admin/article/delete?id=${x._id}`" confirm="delete" @success="remove(index)" class="main bold plr5">
							删除
						</auth-btn>
					</td>
				</tr>
			</block>
		</tables>
		
		<paginate :page="page" :showPageSize="true" @switchPage="changePaginate"></paginate>

	</layout>
</template>

<script>
	export default {
		data() {
			return {
				conditonList:[
					{title:"文章标题",name:"title",type:"text"},
					{title:"文章内容",name:"content",type:"text"},
					{title:"时间",name:"createTimeStart", endName:"createTimeEnd", type:"time" , timeType : "rangetime",valueType:"timestamp"  },
										
				],
				conditions: {},
				loading: true ,
				action: "admin/article/list",
				page: {
					pageNumber: 1,
					lastPage: true,
					totalPage: 1,
					list: [],
					totalRow: 0,
					pageSize: 10
				}
			}
		},
		
		onLoad(params) {
			this.conditions = params ;
		},

		onShow() {
			var that = this ;
			this.getPage();
		},

		methods: {
			
			getPage:function(e){
				this.bcc.call({
					url : this.action ,
					data : {
						...this.conditions,
						pageNumber : this.page.pageNumber ,
						pageSize : this.page.pageSize 
					},
					success : e => {
						uni.hideLoading();
						e.page.list = e.page.list.map(item=>{
							item.createTimeStr = this.bcc.date.toStr(item.publish_date);
							item.article_status=item.article_status==0?"草稿":"发布"
							return item ;
						});
						this.page = e.page ;
						this.conditonList[1].list = e.statusList ;
						this.loading = false ;
					}
				});
			},
			
			submitSearch:function(e){
				this.conditions = e.conditions ;
				this.switchPage();
			},
			
			switchPage: function(pageNumber) {
				uni.showLoading({
					title:"加载中…"
				});
				this.page.pageNumber = pageNumber || this.page.pageNumber ;
				this.getPage();
			},
			
			changePaginate: function(e) {
				this.page.pageSize = e.pageSize ;
				this.page.pageNumber = e.pageNumber ;
				this.switchPage();
			},
			
			remove:function(index){
				// uni.showToast({
				// 	title: '已删除',
				// 	icon : 'success'
				// });
				// this.page.list.splice(index,1);
				this.bcc.clearCache(this.action);
				this.page.list.splice(index,1);
				uni.showToast({
					title: '已删除',
					icon : 'success'
				});
			},
			delete:function(index){
				this.bcc.clearCache(this.action);
				this.page.list.splice(index,1);
				uni.showToast({
					title: '已删除',
					icon : 'success'
				});
			}
		}
	}
</script>
